package net.chanibal.hala.client;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.rpc.SerializableException;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginForm extends Composite {

	private Label errlabel;
	public LoginForm() {

		final FlexTable flexTable = new FlexTable();
		initWidget(flexTable);
		
		final Label urzytkownikLabel = new Label("Urzytkownik:");
		flexTable.setWidget(1, 0, urzytkownikLabel);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		final Label hasloLabel = new Label("Hasło:");
		flexTable.setWidget(3, 0, hasloLabel);
		flexTable.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		final TextBox textBoxUsername = new TextBox();
		flexTable.setWidget(1, 1, textBoxUsername);
		textBoxUsername.setWidth("100%");
		
		final Label errloginlabel = new Label("Nie podałeś nazwy użytkownika:");
		errloginlabel.setStyleName("gwt-Label-Error");
		flexTable.setWidget(0, 1, errloginlabel);
		errloginlabel.setVisible(false);
		
		final Label errhaslolabel = new Label("Nie podałeś Hasła");
		errhaslolabel.setStyleName("gwt-Label-Error");
		flexTable.setWidget(2, 1, errhaslolabel);
		errhaslolabel.setVisible(false);
		
		final PasswordTextBox textBoxPassword = new PasswordTextBox();
		flexTable.setWidget(3, 1, textBoxPassword);
		textBoxPassword.setWidth("100%");

		
		
		final Label statusLabel = new Label("Logowanie...");
		flexTable.setWidget(6, 1, statusLabel);
		statusLabel.setVisible(false);
		
		
		errlabel = new Label("Nieudane połaczenie z serwerem");
		flexTable.setWidget(5, 1, errlabel);
		errlabel.setVisible(false);
		errlabel.setStyleName("gwt-Label-Error");
		
		final Button zalogujButton = new Button();
		flexTable.setWidget(4, 1, zalogujButton);
		zalogujButton.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				errloginlabel.setVisible(false);
				boolean e1 = textBoxUsername.getText().length() == 0;
				errloginlabel.setVisible (e1);
				boolean e2 = textBoxPassword.getText().length() == 0;
				errhaslolabel.setVisible (e2);
				if(e1 || e2) return;
				statusLabel.setVisible(true);
				Server.Util.getInstance().doLogin(
						textBoxUsername.getText(),
						textBoxPassword.getText(),
						new AsyncCallback(){
							public void onFailure(Throwable caught) {
							     // Convenient way to find out which exception was thrown.
							     try {
							       throw caught;
							     } catch (IncompatibleRemoteServiceException e) {
							       // this client is not compatible with the server; cleanup and refresh the 
							       // browser
							    	 errlabel.setText(e.toString());
							     } catch (InvocationException e) {
							       // the call didn't complete cleanly
							    	 errlabel.setText("Błąd połączenia:"+e.toString());
							     } catch (SerializableException e) {
							       // one of the 'throws' from the original method
							    	 errlabel.setText(e.getMessage());
							     } catch (Throwable e) {
							       // last resort -- a very unexpected exception
							    	 errlabel.setText(e.getLocalizedMessage());
							     }
								statusLabel.setVisible(false);
								errlabel.setVisible(true);
							}

							public void onSuccess(Object result) {
								errlabel.setVisible(false);
								Login.isLogined=true;
								statusLabel.setVisible(false);
								History.onHistoryChanged(Login.refergoto);
								Login.refergoto=null;
							}
						
						});
			}
		});
		zalogujButton.setText("Zaloguj");
	}

}
